#!/bin/bash

. /etc/rc.conf
. /etc/conf.d/alsa
. /etc/rc.d/functions

# A function to load the ALSA mixer settings:
load_alsa_mixer() {
  # Volume is muted by default. Set it to 71% unless nosound boot option given:
  if ! cat /proc/cmdline | grep -w nosound >/dev/null 2>/dev/null; then
    if [ -r /etc/asound.state ]; then
	if [ "${msg_lang}" = "cn" ]; then
		echo "设置ALSA混音..."
	else
		echo "Loading ALSA mixer settings..."
	fi
      /usr/sbin/alsactl restore
    else
	if [ "${msg_lang}" = "cn" ]; then
		echo "设置音量: 71%"
	else
		echo "Setting sound volume: 71%"
	fi
      for SCONTROL in {"Master","PCM","Front","Surround","Center","CD","Video"}; do
        /usr/bin/amixer -q sset $SCONTROL 71% unmute 2>/dev/null
      done
    fi
  fi
}

# A function to load the ALSA OSS compat modules:
load_alsa_oss_modules() {
  if ! cat /proc/modules | tr _ - | grep -wq snd-pcm-oss ; then
	if [ "${msg_lang}" = "cn" ]; then
		echo "装载ALSA的OSS兼容模块."
      else
		echo "Loading OSS compatibility modules for ALSA."
	fi
    modprobe snd-pcm-oss
    modprobe snd-mixer-oss
    modprobe snd-seq-oss
  fi
}

load_alsa()
{

# Asus A8Tc声卡特别设置  内核启动参数加入 a8tc
if [ "${a8tc}" = "y" ]; then
	if ! grep "options snd-hda-intel model=6stack" /etc/modprobe.d/modprobe.conf; then
		if [ "${msg_lang}" = "cn" ]; then
			echo "配置Asus A8Tc声卡"
      	else
			echo "Configure for Asus A8Tc sound card."
		fi
		cat >>/etc/modprobe.d/modprobe.conf << "EOF"
options snd-hda-intel model=6stack
EOF
	fi
fi
# If hotplug or something else has loaded the ALSA modules, then
# simply load the mixer settings and make sure the OSS compat
# modules are loaded:
if [ -d /proc/asound ]; then
  load_alsa_mixer
  load_alsa_oss_modules
else
  # If there are ALSA modules defined in /etc/modules.conf, but
  # ALSA is not yet loaded, then load the modules now:
  DRIVERS=`modprobe -c | grep -E "^[[:space:]]*alias[[:space:]]+snd-card-[[:digit:]]" | awk '{ print $3 }'`
  if [ ! "$DRIVERS" = "" ]; then
	if [ "${msg_lang}" = "cn" ]; then
		echo "装载ALSA模块."
	else
		echo "Loading ALSA kernel modules."
	fi
    for module in $DRIVERS; do
      modprobe $module
    done
  fi
  # If ALSA is now up, then load the mixer settings and OSS modules:
  if [ -d /proc/asound ]; then
    load_alsa_mixer
    load_alsa_oss_modules
  fi
fi
}

case "$1" in
  start)
	if [ "${msg_lang}" = "cn" ]; then
		stat_busy "重新载入 音量设置"
	else
		stat_busy "Restoring ALSA Levels"
	fi
    load_alsa
    if [ $? -gt 0 ]; then
      stat_fail
    else
      stat_done
      add_daemon alsa
    fi

    POWERSAVE=${POWERSAVE:-0}
    if [ -e /sys/module/snd_ac97_codec/parameters/power_save \
	 -a $POWERSAVE -ne 0 ]; then
        echo $POWERSAVE > /sys/module/snd_ac97_codec/parameters/power_save
        echo 1 > /dev/dsp
    fi

    if [ -e /sys/module/snd_hda_intel/parameters/power_save \
	 -a $POWERSAVE -ne 0 ]; then
	echo $POWERSAVE > /sys/module/snd_hda_intel/parameters/power_save
        echo 1 > /dev/dsp
    fi
    ;;
  stop)
	if [ "${msg_lang}" = "cn" ]; then
		stat_busy "保存音量设置"
	else
		stat_busy "Saving ALSA Levels"
	fi
    /usr/sbin/alsactl store
    if [ $? -gt 0 ]; then
      stat_fail
    else
      stat_done
      rm_daemon alsa
    fi
    ;;
  restart)
    $0 stop
    sleep 1
    $0 start
    ;;
  *)
    echo "usage: $0 {start|stop|restart}"  
esac
